﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Urbania.Tiles
{
    public class Road : World.Tile
    {

        // ( imageFilename, costMoney, costBureauc, rateIncome, rateHappiness, ratePollution, rateFood )
        //("", 5, 0, 0, 0.2, -5, 0)

        private static Texture2D[] roads;
        private static Texture2D roadBase;
        private static Texture2D roadDefault;

        // Adjacent Indices for tracking which textures to draw.
        public int[] State;
        private int stateIndex;

        public void Init()
        {
            if (roads == null)
            {
                Utilities.Loader.LoadTex("Tiles\\RoadBase");
                Utilities.Loader.LoadTex("Tiles\\RoadDefault");
                roadBase = Utilities.Loader.GetTex("Tiles\\RoadBase");
                roadDefault = Utilities.Loader.GetTex("Tiles\\RoadDefault");
                roads = new Texture2D[6];
                for (int i = 0; i < roads.Length; i++)
                {
                    Utilities.Loader.LoadTex("Tiles\\Road" + i);
                    roads[i] = Utilities.Loader.GetTex("Tiles\\Road" + i);
                }
            }
            State = new int[6];
            stateIndex = 0;
            Type = World.TileType.Road;
            Filename = "";
            FrameRate = 10;
            RepeatFrameIndex = 0;

            CostMoney = 5;
            CostBureauc = 0;
            RateIncome = 0;
            RateHappiness = 0.2;
            RatePollution = -5;
            RateFood = 0;

            this.InitAnimation(FrameRate, RepeatFrameIndex);
        }

        public Road(int xIndex, int yIndex, float xPos, float yPos)
            : base(xIndex, yIndex, xPos, yPos)
        {
            Init();
        }
        public Road(World.Tile t)
            : base(t)
        {
            Init();
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (willDraw)
            {
                int nStates = GetNumOfStates();
                if (nStates == 0)
                {
                    spriteBatch.Draw(roadDefault, PosRender, Color.White);
                }
                else
                {
                    spriteBatch.Draw(roadBase, PosRender, Color.White);
                    for (int i = 0; i < nStates; i++)
                    {
                        spriteBatch.Draw(roads[State[i]], PosRender, Color.White);
                    }
                }
            }
        }

        public void AddNextState(int i)
        {
            State[stateIndex++] = i;
        }

        public void ClearStateArray()
        {
            for (int i = 0; i < State.Length; i++)
            {
                State[i] = -1;
            }
            stateIndex = 0;
        }

        public int GetNumOfStates()
        {
            return stateIndex;
        }

        //public override void Upgrade()
        //{

        //}

    }
}
